'use strict';

const koa = require('koa');
const router = require('koa-router')();
const bodyParser = require('koa-bodyparser');

const app = new koa();

app.use(async (ctx, next) => {
    const {method, url} = ctx.request
    console.log(` ${method} ${url}`)
    await next()
})

router.get('/', async (ctx, next) => {
    // ctx.response.body = "index koa <a href='login'>login</a>"
    ctx.response.body = "<h5>index koa <a href='login'>login</a></h5>"
})

router.get('/login', async (ctx, next) => {
    ctx.response.body = `
        <h2></h2>
        <form action="/signin" method="post">
            <p>name: <input name="name" type="text" /></p>
            <p>password: <input name="password" type="password" /></p>
            <p><input type="submit" value="submit"/></p>
        </form>
    `
})

router.post('/signin', async (ctx, next) => {
    const {name, password} = ctx.request.body
    console.log(name, password)
    if (name === '111' && password === '333') {
        ctx.response.body = 'login success'
    } else {
        ctx.response.body = `
            <a href="/login">try again</a>
        `
    }
})

app.use(bodyParser())

app.use(router.routes())

app.listen(3333)
console.log('app started port 3333')